package com.supermap.dialogs;

import com.supermap.Helper;
import com.supermap.consts.AttrConsts;
import com.supermap.data.*;
import com.supermap.data.Enum;
import com.supermap.dialogs.advance.CJDialog;

import javax.swing.*;
import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.UUID;

public class JoinTableSettingDialog extends CJDialog {
    private JComboBox<String> foreignTableNameCmb;
    private JComboBox<String> srcAttributeCmb;
    private JComboBox<String> foreignTableAttributeCmb;
    private JTextField joinFilterTxt;
    private JComboBox<JoinType> joinTypeCmb;

    private Datasource datasource;
    private DatasetVector currentDatasetVector;
    private JoinItem joinItem;

    public JoinTableSettingDialog(Frame owner, String title, boolean modal, Datasource datasource, DatasetVector currentDatasetVector) {
        super(owner, title, modal, 300, 200, new Object[]{datasource, currentDatasetVector});
    }

    public JoinItem getJoinItem() {
        return joinItem;
    }

    @Override
    protected void initializeArgs(Object[] args) {
        this.datasource = (Datasource) args[0];
        this.currentDatasetVector = (DatasetVector) args[1];
    }

    @Override
    protected void initializeCore() {
        addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosed(WindowEvent e) {
                super.windowClosed(e);
                if (joinItem != null) {
                    joinItem.dispose();
                    joinItem = null;
                }
            }
        });
        setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);

        JPanel settingPanel = new JPanel();
        settingPanel.setLayout(new GridBagLayout());
        add(settingPanel, BorderLayout.CENTER);

        JLabel foreignTableLbl = new JLabel("外接表: ");
        settingPanel.add(foreignTableLbl, new GridBagConstraints(0, 0, 1, 1, 1, 1,
                GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL, Helper.DefaultInsets, 0, 0));

        foreignTableNameCmb = new JComboBox<>();
        settingPanel.add(foreignTableNameCmb, new GridBagConstraints(1, 0, 1, 1, 10, 1,
                GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL, Helper.DefaultInsets, 0, 0));
        Datasets datasets = datasource.getDatasets();
        int datasetsCount = datasets.getCount();
        String currentDatasetName = currentDatasetVector.getName();
        for (int i = 0; i < datasetsCount; i++) {
            Dataset dataset = datasets.get(i);
            String datasetTableName = dataset.getTableName();
            if (dataset instanceof DatasetVector && !datasetTableName.equals(currentDatasetName)) {
                foreignTableNameCmb.addItem(datasetTableName);
            }
        }

        JLabel srcAttributeLbl = new JLabel("本表字段: ");
        settingPanel.add(srcAttributeLbl, new GridBagConstraints(0, 1, 1, 1, 1, 1,
                GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL, Helper.DefaultInsets, 0, 0));

        srcAttributeCmb = new JComboBox<>();
        settingPanel.add(srcAttributeCmb, new GridBagConstraints(1, 1, 1, 1, 10, 1,
                GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL, Helper.DefaultInsets, 0, 0));
        FieldInfos srcFieldInfos = currentDatasetVector.getFieldInfos();
        for (int i = 0; i < srcFieldInfos.getCount(); i++) {
            FieldInfo fieldInfo = srcFieldInfos.get(i);
            srcAttributeCmb.addItem(fieldInfo.getName());
        }

        JLabel foreignTableAttributeLbl = new JLabel("外接表字段: ");
        settingPanel.add(foreignTableAttributeLbl, new GridBagConstraints(0, 2, 1, 1, 1, 1,
                GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL, Helper.DefaultInsets, 0, 0));

        foreignTableAttributeCmb = new JComboBox<>();
        settingPanel.add(foreignTableAttributeCmb, new GridBagConstraints(1, 2, 1, 1, 10, 1,
                GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL, Helper.DefaultInsets, 0, 0));
        if (foreignTableNameCmb.getItemCount() > 0) {
            String selectedDatasetName = (String) foreignTableNameCmb.getSelectedItem();
            DatasetVector datasetVector = (DatasetVector) datasets.get(selectedDatasetName);
            FieldInfos foreignTableFieldInfos = datasetVector.getFieldInfos();
            for (int i = 0; i < foreignTableFieldInfos.getCount(); i++) {
                FieldInfo fieldInfo = foreignTableFieldInfos.get(i);
                foreignTableAttributeCmb.addItem(fieldInfo.getName());
            }
        }

        JLabel joinFilterLbl = new JLabel("关联表达式: ");
        settingPanel.add(joinFilterLbl, new GridBagConstraints(0, 3, 1, 1, 1, 1,
                GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL, Helper.DefaultInsets, 0, 0));

        joinFilterTxt = new JTextField();
        settingPanel.add(joinFilterTxt, new GridBagConstraints(1, 3, 1, 1, 10, 1,
                GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL, Helper.DefaultInsets, 0, 0));
        joinFilterTxt.setEnabled(false);

        JLabel joinTypeLbl = new JLabel("连接类型: ");
        settingPanel.add(joinTypeLbl, new GridBagConstraints(0, 4, 1, 1, 1, 1,
                GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL, Helper.DefaultInsets, 0, 0));

        joinTypeCmb = new JComboBox<>();
        settingPanel.add(joinTypeCmb, new GridBagConstraints(1, 4, 1, 1, 1, 1,
                GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL, Helper.DefaultInsets, 0, 0));
        Enum[] enums = JoinType.getEnums(JoinType.class);
        for (Enum anEnum : enums) {
            joinTypeCmb.addItem((JoinType) anEnum);
        }

        foreignTableNameCmb.addItemListener(e -> {
            String foreignTableName = (String) e.getItem();
            DatasetVector datasetVector = (DatasetVector) datasets.get(foreignTableName);
            FieldInfos fieldInfos = datasetVector.getFieldInfos();
            foreignTableAttributeCmb.removeAll();
            for (int i = 0; i < fieldInfos.getCount(); i++) {
                FieldInfo fieldInfo = fieldInfos.get(i);
                foreignTableAttributeCmb.addItem(fieldInfo.getName());
                reloadJoinFilter();
            }
        });
        srcAttributeCmb.addItemListener(e -> reloadJoinFilter());
        foreignTableAttributeCmb.addItemListener(e -> reloadJoinFilter());

        if (foreignTableNameCmb.getItemCount() > 0 && srcAttributeCmb.getItemCount() > 0 && foreignTableAttributeCmb.getItemCount() > 0) {
            reloadJoinFilter();
        }
    }

    @Override
    protected void doConfirmCore() {
        if (joinItem != null) {
            joinItem.dispose();
        }

        joinItem = null;
        if (foreignTableNameCmb.getItemCount() == 0 || foreignTableAttributeCmb.getItemCount() == 0) {
            return;
        }

        joinItem = new JoinItem();
        joinItem.setForeignTable((String) foreignTableNameCmb.getSelectedItem());
        joinItem.setJoinType((JoinType) joinTypeCmb.getSelectedItem());
        joinItem.setJoinFilter(joinFilterTxt.getText());
        joinItem.setName(UUID.randomUUID().toString());

        setVisible(false);
    }

    private void reloadJoinFilter() {
        if (srcAttributeCmb.getItemCount() == 0 || foreignTableAttributeCmb.getItemCount() == 0) {
            return;
        }

        String srcTableName = currentDatasetVector.getTableName();
        String srcAttribute = (String) srcAttributeCmb.getSelectedItem();
        Object foreignTableName = foreignTableNameCmb.getSelectedItem();
        String foreignTableAttribute = (String) foreignTableAttributeCmb.getSelectedItem();
        joinFilterTxt.setText(srcTableName + AttrConsts.POINT + srcAttribute + AttrConsts.EQUAL +
                foreignTableName + AttrConsts.POINT + foreignTableAttribute);
    }
}
